package com.fingard.dsp.bank.dbobj.paynotice;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import com.fingard.FGBiz;
import com.fingard.crypto.DigestHelper;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.helpers.dbhelpers.InsertHelper;
import com.fingard.dsp.bank.helpers.dbhelpers.LoadHelper;
import com.fingard.dsp.localdb.BizDbType;
import com.fingard.dsp.localdb.dbtable.TableNotice;
import com.fingard.text.StringHelper;

public class NoticeDataHelper {
	private Connection partDbconn = null;
	private String tenant = "";
	
	/**！！注意：需在finally里显示调用close()方法
	 * @param 租户
	 * @param 日期*/
	public NoticeDataHelper(String pTenant, Date pPartDate) throws Exception{
		tenant = pTenant;
		partDbconn = FGBiz.partTable.getDbConnByBiz(pTenant, pPartDate, BizDbType.PayNotice);
	}
	
	public String getTenant(){
		return tenant;
	}
	
	/**写入或更新通知数据*/
	public int insertOrUpdate(SinglePayNotice pData, String pBankCode) throws Exception{
		String tabName = TableNotice.getTableName(pBankCode);
		
		String chkSql = "select count(*) from "+tabName+" where REQSEQID=?";
		Object[] objsChk = new Object[1];
		objsChk[0] = pData.reqSeqID;
		Object chkObj = JDBCHelper.execScalar(partDbconn, chkSql, objsChk);
		int chkCount = -1;
		if(chkObj != null){
			chkCount = Integer.parseInt(chkObj.toString());
		}
		if(chkCount <= 0){
			pData.createdOn = new Date();
			pData.lastModifiedOn = new Date();
			pData.rowVersion = 1;
			pData.digest = getDigest(pData.reqSeqID, pData.transState, pData.payInfo);
			pData.keyVer = 1;
			int rowAff = InsertHelper.insertObject(partDbconn, tabName, pData);
			if(rowAff == 1){
				return 1;
			}else{
				throw new Exception("写入"+tabName+"的影响行数："+rowAff);
			}
		}else{
			
			pData.digest = getDigest(pData.reqSeqID, pData.transState, pData.payInfo);
			String updSql = "update "+tabName+" set TRANSSTATE=?,PAYINFOCODE=?,PAYINFO=?,DIGEST=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where REQSEQID=?";
			Object[] objsUpd = new Object[6];
			objsUpd[0] = pData.transState;
			objsUpd[1] = pData.payInfoCode;
			objsUpd[2] = pData.payInfo;
			objsUpd[3] = pData.digest;
			objsUpd[4] = new Date();
			objsUpd[5] = pData.reqSeqID;
			return JDBCHelper.update(partDbconn, updSql, objsUpd);
		}
	}
	
	public SinglePayNotice loadData(String pReqSeqID, String pBankCode) throws UnsupportedEncodingException, Exception{
		String tabName = TableNotice.getTableName(pBankCode);
		
		String sql = "select * from "+tabName+" where REQSEQID=?";
		Object[] objs = new Object[1];
		objs[0] = pReqSeqID;
		
		Map<String,Object> mapData = JDBCHelper.execQuerySingle(partDbconn, sql, objs);
		if(mapData != null && mapData.size() > 0){
			SinglePayNotice respData = new SinglePayNotice();
			LoadHelper.loadObject(mapData, respData);
			String chkDigest = getDigest(respData.reqSeqID, respData.transState, respData.payInfo);
			if(chkDigest.equals(respData.digest)){
				return respData;
			}else{
				throw new Exception("摘要值"+respData.digest+"与校验值"+chkDigest+"不一致");
			}
		}
		return null;
	}
	
	public String getDigest(String... p_values) throws UnsupportedEncodingException, Exception{
		String dgStr = StringHelper.joinNotEmpty("", p_values)+"97Y#vPdiV@l!sEW$yh_y&63oo";
		return DigestHelper.cryptoSHA1RetHexStr(dgStr.getBytes("utf-8"));
	}
	
	
	
	public void close(){
		if(partDbconn != null){
			try{
				partDbconn.close();
			}catch(Exception ex){
				FGBiz.limitMsg.exception("ReceiptDataHelper.closePartDbConn", ex);
			}
		}
	}
}
